﻿using System.Linq;
using System.Web.Mvc;

namespace dPet.Web.Hospital.Controllers
{
	using dPet.Application.DataObjects;
	using dPet.Application.QueryObjects;
	using dPet.Application.Services;
	using dPet.Web.Hospital.Fliters;
	using dPet.Web.Hospital.ViewModels;
	using Hangerd.Mvc;
	using Hangerd.Mvc.ViewModels;

	public class ProductController : HangerdController
	{
		private const int PRODUCT_LIST_PAGE_SIZE = 15;

		private readonly IProductManagementService _productManagementService;

		public ProductController(IProductManagementService productManagementService)
		{
			_productManagementService = productManagementService;
		}

		#region Product

		[StaffLoginAuth(Privilege = PrivilegesDto.ProductManagement)]
		public ActionResult List(ProductQueryDto queryDto, int pageIndex = 1)
		{
			var page = pageIndex < 1 ? 1 : pageIndex;
			var totalNumber = 0;
			var products = _productManagementService.GetProducts(LoginStaffModel.Current.HospitalId,
				true, true, queryDto, page, PRODUCT_LIST_PAGE_SIZE, out totalNumber);

			ViewBag.QueryDto = queryDto;

			return View(new PagedListModel<ProductDto>
			{
				PageIndex = page,
				PageSize = PRODUCT_LIST_PAGE_SIZE,
				TotalNumber = totalNumber,
				List = products
			});
		}

		[StaffLoginAuth(Privilege = PrivilegesDto.ProductManagement)]
		public ActionResult Index(string id, string barcode = null)
		{
			var loginStaff = LoginStaffModel.Current;

			var product = !string.IsNullOrWhiteSpace(id)
				? _productManagementService.GetProduct(id, true, false)
				: new ProductDto
				{
					HospitalId = loginStaff.HospitalId,
					Barcode = barcode,
					EnableQuantityAlarm = true,
					QuantityAlarmThreshold = 50,
					EnableExpiryAlarm = true,
					ExpiryAlarmThreshold = 7
				};

			if (product == null)
			{
				return RedirectToAction("List", "Product");
			}

			var categories = _productManagementService.GetProductCategories(loginStaff.HospitalId).ToList();

			if (categories.Count == 0)
			{
				return base.AlertAndRedirect("请至少添加一个类别", string.Format("/Product/Category"));
			}

			var subCategories = product.Category == null
				? _productManagementService.GetProductSubCategories(categories.First().Id)
				: _productManagementService.GetProductSubCategories(product.Category.Id);

			return View(new ProductModel
			{
				Product = product,
				Categories = categories,
				SubCategories = subCategories
			});
		}

		[HttpPost]
		[StaffLoginAuth(Privilege = PrivilegesDto.ProductManagement)]
		public ActionResult SaveProduct(ProductDto productDto)
		{
			var loginStaff = LoginStaffModel.Current;

			var result = !string.IsNullOrWhiteSpace(productDto.Id)
				? _productManagementService.UpdateProduct(productDto.Id, productDto, loginStaff)
				: _productManagementService.AddProduct(productDto, loginStaff);

			return base.AlertAndRedirect(result.Message, string.Format("/Product/List"));
		}

		#endregion

		#region Category

		[StaffLoginAuth(Privilege = PrivilegesDto.ProductCategoryManagement)]
		public ActionResult CategoryList()
		{
			var categories = _productManagementService.GetProductCategories(LoginStaffModel.Current.HospitalId);

			return View(categories);
		}

		[StaffLoginAuth(Privilege = PrivilegesDto.ProductCategoryManagement)]
		public ActionResult Category(string id)
		{
			var category = !string.IsNullOrWhiteSpace(id)
				? _productManagementService.GetProductCategory(id)
				: new ProductCategoryDto { HospitalId = LoginStaffModel.Current.HospitalId };

			if (category == null)
			{
				return RedirectToAction("CategoryList", "Product");
			}

			return View(category);
		}

		[HttpPost]
		[StaffLoginAuth(Privilege = PrivilegesDto.ProductCategoryManagement)]
		public ActionResult SaveCategory(ProductCategoryDto categoryDto)
		{
			var loginStaff = LoginStaffModel.Current;

			var result = !string.IsNullOrWhiteSpace(categoryDto.Id)
				? _productManagementService.UpdateProductCategory(categoryDto.Id, categoryDto, loginStaff)
				: _productManagementService.AddProductCategory(categoryDto, loginStaff);

			return base.AlertAndRedirect(result.Message, string.Format("/Product/CategoryList"));
		}

		#endregion

		#region SubCategory

		[StaffLoginAuth(Privilege = PrivilegesDto.ProductCategoryManagement)]
		public ActionResult SubCategoryList(string categoryId)
		{
			var category = _productManagementService.GetProductCategory(categoryId);

			if (category == null)
			{
				return RedirectToAction("CategoryList", "Product");
			}

			var subCategories = _productManagementService.GetProductSubCategories(categoryId);

			ViewBag.CategoryId = category.Id;
			ViewBag.CategoryName = category.Name;

			return View(subCategories);
		}

		[StaffLoginAuth(Privilege = PrivilegesDto.ProductCategoryManagement)]
		public ActionResult SubCategory(string id, string categoryId)
		{
			var category = _productManagementService.GetProductCategory(categoryId);

			if (category == null)
			{
				return RedirectToAction("CategoryList", "Product");
			}

			var subCategory = !string.IsNullOrWhiteSpace(id)
				? _productManagementService.GetProductSubCategory(id, true)
				: new ProductSubCategoryDto { Category = category };

			if (subCategory == null)
			{
				return RedirectToAction("SubCategoryList", "Product", new { categoryId = categoryId });
			}

			return View(subCategory);
		}

		[HttpPost]
		[StaffLoginAuth(Privilege = PrivilegesDto.ProductCategoryManagement)]
		public ActionResult SaveSubCategory(ProductSubCategoryDto subCategoryDto)
		{
			var loginStaff = LoginStaffModel.Current;

			var result = !string.IsNullOrWhiteSpace(subCategoryDto.Id)
				? _productManagementService.UpdateProductSubCategory(subCategoryDto.Id, subCategoryDto, loginStaff)
				: _productManagementService.AddProductSubCategory(subCategoryDto, loginStaff);

			return base.AlertAndRedirect(result.Message, string.Format("/Product/SubCategoryList?categoryId={0}", subCategoryDto.Category.Id));
		}

		#endregion
	}
}
